import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

class TitleNumberField extends StatefulWidget {
  final String title;
  final int value;
  final int min;
  final ValueChanged<int> onChanged;

  const TitleNumberField({
    Key key,
    @required this.title,
    this.value = 1,
    this.min = 0,
    this.onChanged,
  }) : super(key: key);

  @override
  _TitleNumberFieldState createState() => _TitleNumberFieldState();
}

class _TitleNumberFieldState extends State<TitleNumberField> {
  TextEditingController _controller;

  int get _number {
    return int.parse(_controller.text);
  }

  bool get _isShowSubtract {
    return _number > widget.min;
  }

  @override
  void initState() {
    super.initState();

    _controller = TextEditingController(text: '${widget.value}');

    _controller.addListener(_numberKeyHandle);
  }

  _numberKeyHandle() {
    if (_controller.text == '') {
      _controller.text = '1';
    }

    if (widget.onChanged != null) {
      widget.onChanged(_number);
    }
  }

  @override
  void dispose() {
    _controller.removeListener(_numberKeyHandle);
    _controller.dispose();

    super.dispose();
  }

  _subtract() {
    setState(() {
      var text = '${_number - 1}';

      _controller.value = _controller.value.copyWith(
        text: text,
        selection: TextSelection.fromPosition(TextPosition(offset: text.length)),
      );
    });

    if (widget.onChanged != null) {
      widget.onChanged(_number);
    }
  }

  _plus() {
    setState(() {
      var text = '${_number + 1}';

      _controller.value = _controller.value.copyWith(
        text: text,
        selection: TextSelection.fromPosition(TextPosition(offset: text.length)),
      );
    });

    if (widget.onChanged != null) {
      widget.onChanged(_number);
    }
  }

  @override
  Widget build(BuildContext context) {
    return CustomListTitle(
      title: widget.title,
      trailing: Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          _isShowSubtract
              ? GestureDetector(
                  child: BaseImage(
                    'assets/image/cut_icon.png',
                    width: 34.w,
                    height: 34.w,
                  ),
                  onTap: _subtract,
                )
              : Container(),
          Container(
            width: 70.w,
            child: TextFormField(
              keyboardAppearance: Brightness.light,
              controller: _controller,
              textAlign: TextAlign.center,
              decoration: InputDecoration(
                border: NoInputBorder(),
                enabledBorder: NoInputBorder(),
                focusedBorder: NoInputBorder(),
                contentPadding: EdgeInsets.all(0),
              ),
              keyboardType: TextInputType.number,
            ),
          ),
          GestureDetector(
            child: BaseImage(
              'assets/image/plus_icon.png',
              width: 34.w,
              height: 34.w,
            ),
            onTap: _plus,
          ),
        ],
      ),
    );
  }
}
